t_s = 0.01; %采样周期
t_start = 0.5;  %起始时间
t_end = 5;      %结束时间
t = t_start : t_s : t_end;
y = 1.5*sin(2*pi*5*t)+3*sin(2*pi*20*t)+randn(1,length(t));  %生成信号

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%频谱%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y_f = fft(y);   %傅里叶变换
subplot(5, 1, 1);
plot(t, y);title('original signal');    %绘制原始信号图
Druation = t_end - t_start; %计算采样时间
Sampling_points = Druation/t_s + 1;    %采样点数,fft后的点数就是这个数
f_s = 1/t_s;    %采样频率
f_x = 0:f_s/(Sampling_points - 1):f_s;  %注意这里和横坐标频率对应上，频率分辨率f_s/(Sampling_points - 1)
t2 = f_x - f_s/2;
shift_f = abs(fftshift(y_f));
subplot(5, 1, 2);
plot(f_x,abs(y_f));title('fft transform');
subplot(5,1,3);
plot(f_x-f_s/2, shift_f);title('shift fft transform');  %将0频率分量移到坐标中心
subplot(5,1,4);
plot(t2(length(t2)/2:length(t2)),shift_f(length(shift_f)/2:length(shift_f)));title('shift fft transform');    %保留正频率部分
subplot(5,1,5);
plot(f_x(1:length(f_x)/2), abs(y_f(1:length(f_x)/2)));title('fft cut'); %直接截取fft结果的前半部分